package xuhaijia.lanou3g.autohome.recommend.newest;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.util.Log;
import android.widget.ListView;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.custom.MyListener;
import xuhaijia.lanou3g.autohome.custom.PullToRefreshLayout;
import xuhaijia.lanou3g.autohome.recommend.recommendreusing.RecommendBean;
import xuhaijia.lanou3g.autohome.turnpage.TurnPagerRightDownAdapter;
import xuhaijia.lanou3g.autohome.turnpage.TurnPageRightDown;

/**
 * Created by dllo on 16/8/15.
 */
public class NewestFragment extends BaseFragment {

    private ListView lv;
    private NewestAdapter newestAdapter;
    private TurnPagerRightDownAdapter vpAdapter;
    private TurnPageRightDown turnPageRightDown;
    private PullToRefreshLayout ptfl;
    int count = 1;


    @Override
    protected int setLayout() {
        return R.layout.fragment_listview;
    }

    @Override
    protected void initView() {
        ptfl = bindView(R.id.ptrl);
        MyListener myListener = new MyListener(this);
        ptfl.setOnRefreshListener(myListener);
        lv = bindView(R.id.lv);
    }


    @Override
    protected void initData() {

        turnPageRightDown = new TurnPageRightDown(mContext);
        newestAdapter = new NewestAdapter(mContext);
        vpAdapter = new TurnPagerRightDownAdapter(mContext);
        mNetTool.getNetData(URLValues.NEW_URL, RecommendBean.class, new NetTool.NetListener<RecommendBean>() {
            @Override
            public void onSuccess(RecommendBean recommendBean) {
                newestAdapter.setArrayList(recommendBean);
                lv.setAdapter(newestAdapter);

                turnPageRightDown.setRecommendBean(recommendBean);
                lv.addHeaderView(turnPageRightDown.getView());
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void load() {
        String urlString = URLValues.NEW_URL;
        //http://app.api.autohome.com.cn/autov4.2.5/news/newslist-a2-pm1-v4.2.5-c0-nt0-p1-s30-l0.html
        int start = urlString.indexOf("http");
        int middle = urlString.indexOf("1-s");
        int end = urlString.indexOf("-s30");
        count++;
        String urlStart = urlString.substring(start, middle);
        String urlEnd = urlString.substring(end , urlString.length());
        String urlFinal = urlStart + count + urlEnd;
        mNetTool.getNetData(urlFinal, RecommendBean.class, new NetTool.NetListener<RecommendBean>() {
            @Override
            public void onSuccess(RecommendBean recommendBean) {
                newestAdapter.addArrayList(recommendBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void refresh() {
        mNetTool.getNetData(URLValues.NEW_URL, RecommendBean.class, new NetTool.NetListener<RecommendBean>() {
            @Override
            public void onSuccess(RecommendBean bean) {
                newestAdapter.setArrayList(bean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
        turnPageRightDown.stop();
    }



}
